<?php
namespace app\controllers;

use app\common\SendMailer;
use app\logic\UserLogic;
use yii;

class PartnerController extends BackendController
{
    const LIMIT=30;

    public function actionIndex(){
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $userLogic = new UserLogic();
        $this->data['breadcrumbs'] = '合作商管理';
        return $this->render('index', [
            'partner' => $userLogic->getAllPartner($query, $page),
            'query' => $query
        ]);
    }

    public function actionAdd()
    {
        $userLogic = new UserLogic();
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['合作商管理' => U('partner/index'), ' 添加合作商' => ''];
            return $this->render('add', [
                'theme' => '/assets/',
                'actionUrl' => U('partner/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $logicRes = $userLogic->addPartner($params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增合作商成功');
                jump('partner/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增合作商失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionEdit()
    {
        $userLogic = new UserLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('partner_id');
            $partner = $userLogic->getPartnerById($id);
            $this->data['breadcrumbs'] = ['合作商管理' => U('partner/index'), $partner['partner_name'] => '', '更新' => ''];
            return $this->render('edit', [
                'partner'=>$partner,
                'theme' => '/assets/',
                'actionUrl' => U('partner/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('partner_id');
            $logicRes = $userLogic->editPartnerById($id, $params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新合作商成功');
                jump('partner/index');
            } else {
                Yii::$app->getSession()->setFlash('error','更新合作商失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }

    public function actionSaveStatus()
    {
        $id = Yii::$app->request->get('partner_id');
        $userLogic = new UserLogic();
        $logicRes = $userLogic->savePartnerStatus($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','更改合作商状态成功');
        } else {
            Yii::$app->getSession()->setFlash('error','更改合作商状态失败');
        }
        return $this->redirect(U('partner/index'));
    }

    public function actionSendPassword(){
        $id = Yii::$app->request->post('partner_id');
        $userLogic = new UserLogic();
        $partner = $userLogic->getPartnerById($id);
        $sendMailer = new SendMailer();
        $res = $sendMailer->send($partner['linkemail'],$partner['partner_name'],$partner['ftp_password']);
        echo json_encode($res);
    }


}